﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SpanDemo
{
    public class Program
    {
        public unsafe static void Main(string[] args) 
        {
            //引用托管堆内存:
            var array = new long[10];
            Span<long> sArr1 = new Span<long>(array);
            sArr1.Slice(start: 1, length: 5).Fill(211);
            Console.WriteLine(String.Join(" | ", sArr1.ToArray()));

            //引用本机非托管内存:
            var native = Marshal.AllocHGlobal(sizeof(long) * 10);
            long* ptr = (long*)native.ToPointer();
            Span<long> sArr3 = new Span<long>(ptr, 10);
            sArr3.Slice(start: 1, length: 5).Fill(985);
            Console.WriteLine(String.Join(" | ", sArr3.ToArray()));

            //引用栈内存:
            Span<long> sArr2 = stackalloc long[10];
            sArr2.Slice(start: 1, length: 5).Fill(996);
            Console.WriteLine(String.Join(" | ", sArr2.ToArray()));

            //检测引用重叠
            Span<long> sTemp = sArr1.Slice(2, 5);
            Console.WriteLine("sArr1 and sTemp is overlaps:{0}",sArr1.Overlaps(sTemp));

            //查找索引
            Console.WriteLine("IndexOf:" + sArr1.IndexOf(sTemp));

            //逆置(直接改变堆上的)
            sArr1.Reverse();
            Console.WriteLine(String.Join(" | ", sArr1.ToArray()));
            Console.WriteLine(String.Join(" | ", array));


        }

    }
}
